package hong;

import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.util.StringConverter;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.regex.Pattern;

/**
 * 时、分、秒 max==99:60:60==36,0060 s
 */
public class TimeDesktopCtrl  extends BorderPane implements Initializable {

    public static TimeDesktopCtrl tDesktop;
    public static Map<Integer,Map> timeParams = new HashMap<>();
    /** -1 添加 ，不等于-1 编辑 */
    public static int editID = -1;

    @FXML
    Pane timeDesktop;
    @FXML
    Pane panelAdd;
    @FXML
    Pane panelAddTable;
    @FXML
    ScrollPane timeScroll;
    @FXML
    FlowPane timeTable;
    @FXML
    ImageView btnAdd;
    @FXML
    ImageView btnAddOk;
    @FXML
    ImageView btnAddDel;
    @FXML
    TextField timeTitle;
    @FXML
    TextField timeHour;
    @FXML
    TextField timeMinute;
    @FXML
    TextField timeSecond;
    @FXML
    TextField timeRefresh;



    public static String patternNumStr = "[\\d]{1,2}";//时分秒
    public static String patternNum2Str = "[\\d]{1,5}";//刷新时间
    /** title,hour,minute,second */
    Pattern patternNum = Pattern.compile(patternNumStr);
    Pattern patternNum2 = Pattern.compile(patternNum2Str);

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        tDesktop = this;
        initLayout();
        initEvent();
    }

    /**
     *
     */
    public void changeTimeTitle(){
        String str = timeTitle.getText();
        if(str != null && str.length() > 10){
            timeTitle.setText(str.substring(0,10));
        }
    }
    public void initEvent(){
        timeHour.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue)->{
            if(!inputIsNum(newValue)){
                timeHour.setText(oldValue);
            }else if(!inputIs99(newValue)){
                timeHour.setText("99");
            }
        });
        timeMinute.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue)->{
            if(!inputIsNum(newValue)){
                timeMinute.setText(oldValue);
            }else if(!inputIs99(newValue)){
                timeMinute.setText("99");
            }
        });
        timeSecond.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue)->{
            if(!inputIsNum(newValue)){
                timeSecond.setText(oldValue);
            }else if(!inputIs99(newValue)){
                timeSecond.setText("99");
            }
        });
        timeRefresh.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue)->{
            if(!patternNum2.matcher(newValue).matches()){
                timeRefresh.setText("200");
            }
            int i = Integer.parseInt(newValue);
            if(i < 100 || i > 10000){//刷新时间间隔：0.1s~10s
                timeRefresh.setText("200");
            }
        });
//        timeTitle.setTextFormatter(new TextFormatter<String>(new StringConverter<String>() {
//            @Override
//            public String toString(String object) {
//                if("a".equals(object)){
//                    return "a--1";
//                }
//                return object;
//            }
//
//            @Override
//            public String fromString(String string) {
//                if("b".equals(string)){
//                    return "b--1";
//                }
//                return string;
//            }
//        }));
    }
    /**
     * 根据屏幕大小调整各组件布局
     */
    public void initLayout(){
        timeDesktop.setMaxSize(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT);
        timeDesktop.setPrefSize(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT);
        timeScroll.setMaxSize(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT);
        timeScroll.setPrefSize(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT);
        timeTable.setMaxSize(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT);
        timeTable.setPrefSize(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT);
        //倒计时宽高：260x300
        int tmp = (Constants.SCREEN_WIDTH - 30)%260/2;
        Insets insets = new Insets(16,0,0,tmp);
        timeTable.setPadding(insets);
        btnAdd.setLayoutX(Constants.SCREEN_WIDTH - 150);
        btnAdd.setLayoutY(Constants.SCREEN_HEIGHT - 150);
        panelAdd.setMaxSize(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT);
        panelAdd.setPrefSize(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT);
        //倒计时添加-表格-宽高：300x260
        int tmpX = (Constants.SCREEN_WIDTH - 300)/2;
        int tmpY = (Constants.SCREEN_HEIGHT - 260)/2;
        panelAddTable.setLayoutX(tmpX);
        panelAddTable.setLayoutY(tmpY);
    }

    /**
     * TimeDownCtrl里调用
     * @param timePanel
     */
    public void timeDelete(Pane timePanel){
        timeTable.getChildren().remove(timePanel);
    }

    public void timeAdd(){
        resetTextField();
        panelAdd.setVisible(true);
    }
    public void changeTitle(){}
//    public void changeHour(){
//        if(!inputIsNum(timeHour.getText())){
//            timeHour.setText("");
//        }else if(!inputIs99(timeHour.getText())){
//            timeHour.setText("99");
//        }
//    }
//    public void changeMinute(){
//        if(!inputIsNum(timeMinute.getText())){
//            timeMinute.setText("");
//        }else if(!inputIs99(timeMinute.getText())){
//            timeMinute.setText("99");
//        }
//    }
//    public void changeSecond(){
//        if(!inputIsNum(timeSecond.getText())){
//            timeSecond.setText("");
//        }else if(!inputIs99(timeSecond.getText())){
//            timeSecond.setText("99");
//        }
//    }
    public void timeOk(){
        if(!inputIsOk()){
            return;
        }
        try {
            Map<String,String> param;
            if(editID == -1){
                param = new HashMap<>();
                param.put("editID",timeParams.size() + 1 + "");
            }else{
                param = timeParams.get(editID);
            }
            param.put("title",timeTitle.getText());
            param.put("hour",timeHour.getText());
            param.put("minute",timeMinute.getText());
            param.put("second",timeSecond.getText());
            param.put("timeRefresh",timeRefresh.getText());
            if(editID == -1){
                timeParams.put(timeParams.size() + 1,param);
                Pane timePanel = FXMLLoader.load(Constants.timeDownLayoutFXML);
                timeTable.getChildren().add(timePanel);
            }else{
                editID = -1;//重置编辑ID
                TimeDownCtrl.timeDownEdit.initTimes();
            }
            panelAdd.setVisible(false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void timeEdit(){
        Map<String,String> param = timeParams.get(editID);
        timeTitle.setText(param.get("title"));
        timeHour.setText(param.get("hour"));
        timeMinute.setText(param.get("minute"));
        timeSecond.setText(param.get("second"));
        timeRefresh.setText(param.get("timeRefresh"));
        panelAdd.setVisible(true);
    }

    public boolean inputIsNum(String str){
        return patternNum.matcher(str).matches();
    }
    public boolean inputIs99(String str){
        return Integer.parseInt(str) <= 99;
    }
    public boolean inputIsOk(){
        return inputIsNum(timeHour.getText())
                && inputIsNum(timeMinute.getText())
                && inputIsNum(timeSecond.getText())
                && inputIs99(timeHour.getText())
                && inputIs99(timeMinute.getText())
                && inputIs99(timeSecond.getText());
    }
    public void timeClose(){
        editID = -1;
        panelAdd.setVisible(false);
    }
    /**
     * 重置输入框内容
     */
    public void resetTextField(){
        timeTitle.setText("");
        timeHour.setText("00");
        timeMinute.setText("00");
        timeSecond.setText("00");
//        timeRefresh.setText("200");
    }
}
